package com.threadmethod.synchronize;

import com.threadmethod.SubThread;

/**
 * @PackageName:com.threadmethod.synchronize
 * @ClassName:Test14
 * @Description: 脏读
 *               出现读取属性值出现了意外 读取的时中间值而不是修改之后的值
 * @Author: zhangxiaoyu
 * @date 2020/12/9 17:35
 */
public class Test14 {

    public static void main(String[] args) throws InterruptedException {

        //开启子线程设置用户名和密码
        PublicValue publicValue = new PublicValue();
        SubThread t1 = new SubThread(publicValue);
        t1.start();

        //为了确定设置成功
        Thread.sleep(100);
        publicValue.getValue();
    }


    //定义线程 设置用户名和密码
    static  class  SubThread extends Thread{

       private PublicValue publicValue;

       public SubThread(PublicValue publicValue){
           this.publicValue = publicValue;
       }

        @Override
        public void run() {
            publicValue.setValue("busadaaaaa","123");
        }
    }




    static  class PublicValue{
        private String name = "wkcto";
        private String pwd = "33333";

        public synchronized void getValue(){
            System.out.println(Thread.currentThread().getName()+",getter----name:"+name+",---pwd:"+pwd);
        }

        public  synchronized void  setValue(String name,String pwd){
            this.name =name;

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            this.pwd = pwd;
            
            System.out.println(Thread.currentThread().getName()+",setter --name:"+name+",---pwd:"+pwd);
        }
    }

}
